package cn.bookcycle.panda.user.controller;

import cn.bookcycle.panda.user.util.Dao;
import cn.bookcycle.panda.user.pojo.DO.UserDO;
import cn.bookcycle.panda.user.service.UserService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;

import java.util.List;


/**
 * @author Mason
 * @date 2018/3/27
 */
@RequestMapping("/user")
@RestController
public class UserController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    UserService userService;
    @Autowired
    Dao<Object> userDao;

    /**
     * 通过用户ID找其信息
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getUserById/{userId}",method = RequestMethod.GET)
    public ResponseEntity<Object> getUserById(@PathVariable("userId") String userId){
        try {

            Object userDO = userService.getUserById(userId);
            if(userDO !=null){
                return ResponseEntity.status(HttpStatus.OK).body(userDO);
            }else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     *
     * @param userDO
     * @return
     */
    @RequestMapping(value = "/updateUser",method = RequestMethod.PUT)
    public ResponseEntity<Void> updateUser(@RequestBody Object userDO){
        try {
            if(userService.updateUser(userDO)){
                return ResponseEntity.status(HttpStatus.OK).body(null);
            }else {
                return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(null);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     *
     * @param userDO
     * @return
     */
    @RequestMapping(value = "/saveUser",method = RequestMethod.POST)
    public ResponseEntity<Void> saveUser(@RequestBody Object userDO){
        try {
            if(userService.saveUser(userDO)){
                logger.info(userDO.toString());
                return ResponseEntity.status(HttpStatus.CREATED).body(null);
            }else {
                return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(null);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     *
     * @return
     */
    @RequestMapping(value = "/listUsers",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<List> listUsers(@RequestBody Object userDO){
        try{
            List userDOList=userService.listUser(userDO);
            return ResponseEntity.status(HttpStatus.FOUND).body(userDOList);
        }catch (HttpClientErrorException e){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

}
